package com.yyz.lease.web.app.service.impl;

import com.yyz.lease.model.entity.LeaseAgreement;
import com.yyz.lease.model.entity.LeaseTerm;
import com.yyz.lease.model.entity.PaymentType;
import com.yyz.lease.web.app.mapper.LeaseAgreementMapper;
import com.yyz.lease.web.app.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yyz.lease.web.app.vo.agreement.AgreementDetailVo;
import com.yyz.lease.web.app.vo.agreement.AgreementItemVo;
import com.yyz.lease.web.app.vo.apartment.ApartmentDetailVo;
import com.yyz.lease.web.app.vo.room.RoomDetailVo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @description 针对表【lease_agreement(租约信息表)】的数据库操作Service实现
 */
@Service
@RequiredArgsConstructor
public class LeaseAgreementServiceImpl extends ServiceImpl<LeaseAgreementMapper, LeaseAgreement>
        implements LeaseAgreementService {
    private final LeaseAgreementMapper leaseAgreementMapper;
    private final ApartmentInfoService apartmentInfoService;
    private final RoomInfoService roomInfoService;
    private final PaymentTypeService paymentTypeService;
    private final LeaseTermService leaseTermService;

    /**
     * 获取个人租约基本信息列表
     */
    @Override
    public List<AgreementItemVo> listItem(String phone) {
        return leaseAgreementMapper.listItem(phone);
    }

    /**
     * 根据id获取租约详细信息
     */
    @Override
    public AgreementDetailVo getDetailById(Long id) {
        LeaseAgreement leaseAgreement = getById(id);

        ApartmentDetailVo apartmentDetailVo = apartmentInfoService.getDetailById(leaseAgreement.getApartmentId());
        RoomDetailVo roomDetailVo = roomInfoService.getDetailById(leaseAgreement.getRoomId());
        PaymentType paymentType = paymentTypeService.getById(leaseAgreement.getPaymentTypeId());
        LeaseTerm leaseTerm = leaseTermService.getById(leaseAgreement.getLeaseTermId());

        AgreementDetailVo agreementDetailVo = new AgreementDetailVo();
        BeanUtils.copyProperties(leaseAgreement,agreementDetailVo);
        agreementDetailVo.setApartmentName(apartmentDetailVo.getName());
        agreementDetailVo.setApartmentGraphVoList(apartmentDetailVo.getGraphVoList());
        agreementDetailVo.setRoomNumber(roomDetailVo.getRoomNumber());
        agreementDetailVo.setRoomGraphVoList(roomDetailVo.getGraphVoList());
        agreementDetailVo.setPaymentTypeName(paymentType.getName());
        agreementDetailVo.setLeaseTermMonthCount(leaseTerm.getMonthCount());
        agreementDetailVo.setLeaseTermUnit(leaseTerm.getUnit());

        return agreementDetailVo;
    }
}




